package com.guoguo.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.guoguo.base.BaseController;
import com.guoguo.service.IShortUrlMongoService;
import com.guoguo.utils.PropertyUtils;

/**
 * Created by Administrator on 2017/10/30 0030.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
@RestController
@RequestMapping("/")
public class ShortUrlController extends BaseController {
	
	private final Logger logger = LoggerFactory.getLogger(InviteController.class);
	
	@Autowired
    private IShortUrlMongoService shortUrlMongoService;

    /**
     * 获取原始长的URL
     * @return
     */
    @RequestMapping(value = "/s/{code}", method = RequestMethod.GET)
    public ModelAndView getOldUrl(@PathVariable("code") String code) {
		logger.info("into ShortUrlController getOldUrl method");
    	String oldUrl = null;
        try {
        	oldUrl = shortUrlMongoService.getOldUrl(PropertyUtils.getProperty("api.properties", "api.shortUrl.url")+code);
        	return new ModelAndView(new RedirectView(oldUrl));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
        
    }
}
